const User = require('../../db/userSchame');
const md5 = require('md5');
module.exports = async(req, res) => {
    console.log('登录呀用户名验证');

    let usernameRel = await User.findOne({ "username": req.body.username });
    console.log(usernameRel);
    // 当前查询数据库:1.如果没有找到就是null,2.找到了,查找到整条数据的{}返回
    if (!usernameRel) {
        res.send('<script>alert("用户名不存在");location.href="/admin/login"</script>');
    } else {

        let userInfo = {
            username: req.body.username,
            password: md5(req.body.password)
        };
        var allrel = await User.findOne(userInfo);
        if (allrel) {
            // 设置session的验证
            req.session.username = req.body.username;
            // 本地共享的数据
            req.app.locals.username = req.body.username;
            res.render('./admin/index.ejs');
        } else {
            res.send('<script>alert("密码错误,请重新登录");location.href="/admin/login"</script>');
        }
    };

}